package com.cn.shop.controller;

import com.cn.shop.entity.User;
import com.cn.shop.service.LoginService;
import com.cn.shop.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 公共控制器
 *
 * @author suhao
 * @time 2017.09.18
 * @email suhao755@gmail.com
 */
@RestController
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private LoginService loginService;
    private static Logger log = LoggerFactory.getLogger(CommonController.class);

    /*登录、注册*/
    @PostMapping("/login")
    public ResultBean<String> login(User user, BindingResult bindingResult, HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            throw new CheckException(CHECKMSG.VALIDATE_ARGUMENT_ERROR);
        }
        if (null == user.getUsername() || null == user.getPassword()) {
            throw new CheckException(CHECKMSG.THE_ACCOUNT_WITH_THIS_KEYCODE_WAS_NOT_NULL);
        }
        if (null == user.getImgCode()) {
            throw new CheckException(CHECKMSG.THE_CAPTCHA_CANNOT_BE_EMPTY);
        }
        HttpSession session = request.getSession();
        String sessionImgCode = (String) session.getAttribute(CHECKMSG.RANDOMCODEKEY);
        if (!user.getImgCode().equalsIgnoreCase(sessionImgCode)) {
            throw new CheckException(CHECKMSG.VERIFICATION_CODE_ERROR);
        }
        int userType = loginService.registerUser(user);
        if (userType == 1) {
            return new ResultBean(JwtUtil.generateToken(user.getUsername()),CHECKMSG.LOGIN_SUCCESSFULLY);
        } else {
            return new ResultBean(JwtUtil.generateToken(user.getUsername()),CHECKMSG.REGISTERED_SUCCESSFULLY);
        }
    }

    /*获取验证码*/
    @PostMapping("/getImgCode")
    public ResultBean<String> getImgCode(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        String buffer = "data:image/png;base64," + VerfityCode.getRandcode(request, response);
        return new ResultBean(buffer);
    }

    /*忘记密码*/
    @PostMapping("/forget")
    public ResultBean<String> forget(User user, BindingResult bindingResult, HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            throw new CheckException(CHECKMSG.VALIDATE_ARGUMENT_ERROR);
        }
        if (null == user.getUsername() || null == user.getPassword()) {
            throw new CheckException(CHECKMSG.THE_ACCOUNT_WITH_THIS_KEYCODE_WAS_NOT_NULL);
        }
        if (null == user.getImgCode()) {
            throw new CheckException(CHECKMSG.THE_CAPTCHA_CANNOT_BE_EMPTY);
        }
        HttpSession session = request.getSession();
        String sessionImgCode = (String) session.getAttribute(CHECKMSG.RANDOMCODEKEY);
        if (!user.getImgCode().equalsIgnoreCase(sessionImgCode)) {
            throw new CheckException(CHECKMSG.VERIFICATION_CODE_ERROR);
        }
        loginService.forget(user);
        return new ResultBean(CHECKMSG.SEND_SUCCESSFULLY);
    }
}
